package code.android.MiracleWalker.activity;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import java.util.List;

import code.android.MiracleWalker.R;
import code.android.MiracleWalker.service.MwService;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemSelectedListener;

public class MwPhotoGalleryActivity extends Activity 
{
	public static final String DEBUG_TAG = "MwPhotoGalleryActivity";
	
	private static Bitmap mBitmap;

	Intent						mIntent		= null;
		
	Gallery						mGallery	= null;
	
	ImageView					mImageView 	= null;
	
	private static List<Bitmap> mBitmapContextList		= null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		Log.i(DEBUG_TAG, "onCreate()");
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_gallery_layout);
		
		LinearLayout ll = (LinearLayout) findViewById(R.id.LinearLayout01);
		
		ll.setBackgroundResource(R.drawable.cork_bg);
		
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		mGallery = (Gallery) findViewById(R.id.gallery);
		mImageView = (ImageView) findViewById(R.id.gallery_image_view);
		mBitmapContextList = new ArrayList<Bitmap>();		
		mIntent = new Intent(this, MwService.class);
		
		boolean b = getIntent().getExtras().getBoolean("photos_on_map");
		if(b == false)
		{
			new Handler().post(new Runnable() 
			{
				@Override
				public void run() 
				{
					try 
					{
						List<String> photoList = getPhotoPath();
						
						mGallery.setAdapter( new ImageAdapter(MwPhotoGalleryActivity.this, photoList));
					
						mGallery.setOnItemSelectedListener(new OnItemSelectedListener() 
						{
							@Override
							public void onItemSelected(AdapterView<?> arg0,	View arg1, int arg2, long arg3) 
							{
								mBitmap = mBitmapContextList.get(arg2);
//								if(mBitmap.isRecycled() == false)
//								{
//									mBitmap.recycle();
//								}
								
								mImageView.setImageBitmap(Bitmap.createScaledBitmap(mBitmap, 480, 320, true));
								
								Log.d(DEBUG_TAG, "aa: "+String.valueOf(arg2));
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0)
							{
							}
							
						});
						
						registerForContextMenu(mGallery);
					} 
					catch ( NullPointerException e) 
					{
					}
				}
			});
		}
		else
		{
			new Handler().post(new Runnable() 
			{
				@Override
				public void run() 
				{
					try 
					{
						String photos = getIntent().getExtras().getString("photos");
						String[] path = photos.split("&&");
						List<String> photoList = new ArrayList<String>();
						for(String element : path)
						{
							photoList.add(element);
							Log.d(DEBUG_TAG, element);
						}
						
						mGallery.setAdapter( new ImageAdapter(MwPhotoGalleryActivity.this, photoList));
					
						mGallery.setOnItemSelectedListener(new OnItemSelectedListener() 
						{
							@Override
							public void onItemSelected(AdapterView<?> arg0,	View arg1, int arg2, long arg3) 
							{
								mBitmap = mBitmapContextList.get(arg2);
//								if(mBitmap.isRecycled() == false)
//								{
//									mBitmap.recycle();
//								}
								
								mImageView.setImageBitmap(Bitmap.createScaledBitmap(mBitmap, 480, 320, true));
								
								Log.d(DEBUG_TAG, "aa: "+String.valueOf(arg2));
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0)
							{
							}
							
						});
						
						registerForContextMenu(mGallery);
					} 
					catch ( NullPointerException e) 
					{
					}
				}
			});
			
		}
		

	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
	}
	
	private List<String> getPhotoPath()
	{
		List<String> list = new ArrayList<String>();
		
		try 
		{
			String thisLine;
			
			FileReader fr = new FileReader(getFileStreamPath(MwService.PHOTO_PATH_TEMP));
			
			BufferedReader br = new BufferedReader(fr);
			
			 while ((thisLine = br.readLine()) != null)
			 {
				 	list.add(thisLine);
			 }
		} 
		catch (FileNotFoundException e1) 
		{
			e1.printStackTrace();
		}
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		
		return list;
	}
	
	public class ImageAdapter extends BaseAdapter 
	{
		int				mGalleryItemBackground	= 0;
		List<String>	mPhotoList				= null;
		private Context	mContext				= null;

		public ImageAdapter(Context c, List<String> photoList) 
		{
			mContext = c;
			mPhotoList = photoList;
			// See res/values/attrs.xml for the <declare-styleable> that defines
			// Gallery1.
			TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
			mGalleryItemBackground = a.getResourceId( R.styleable.Gallery1_android_galleryItemBackground, 0);
			a.recycle();
		}

		public int getCount() 
		{
			return mPhotoList.size();
		}

		public String getItem(int position) 
		{
			return mPhotoList.get(position);
		}

		public long getItemId(int position) 
		{
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) 
		{
			ImageView i = new ImageView(mContext);
			
			String[] photoPath = mPhotoList.get(position).split(";");
				
			BitmapFactory.Options ops = new BitmapFactory.Options();
			ops.inSampleSize = 8;
			
			Bitmap b = BitmapFactory.decodeFile(photoPath[0].substring(7), ops);
			
			mBitmapContextList.add(position, b);
			
			Log.d(DEBUG_TAG, "dd: "+String.valueOf(position));
			
			i.setImageBitmap(Bitmap.createScaledBitmap(b, 150, 100, true));
			i.setScaleType(ImageView.ScaleType.FIT_XY);
			i.setLayoutParams(new Gallery.LayoutParams(150, 100));
			i.setBackgroundResource(mGalleryItemBackground);
			
			return i;
		}
	}
}
